/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.logisoft.db.domain;

import com.core.logisoft.db.DBConnection;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class Domain {

    protected String OrgId;
    public String me = "sun";

    public static void main(String[] args) {
        new Domain().getQuery();
    }

    protected void setPropertiesFromRs(ResultSet rs) throws SQLException {
        for (Field f : getClass().getDeclaredFields()) {
            try {
                f.set(this, rs.getString(f.getName()));
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Domain.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Domain.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void save() throws SQLException {
        String query = getQuery();       
        DBConnection.getConnection().setAutoCommit(true);
        DBConnection.getConnection().createStatement().executeUpdate(query);
    }

    private String getQuery() {
        StringBuilder queryBuffer = new StringBuilder();
        queryBuffer.append("INSERT INTO ");
        queryBuffer.append(getClass().getSimpleName());
        queryBuffer.append(" (");

        StringBuilder columnNames = new StringBuilder();
        StringBuilder columnValues = new StringBuilder();
        boolean isFirst = true;
        Field[] fields = getClass().getDeclaredFields();
        for (Field f : fields) {
            try {
                if (isFirst) {
                    isFirst = false;
                    columnNames.append(f.getName());

                    columnValues.append("'");
                    columnValues.append(f.get(this));
                    columnValues.append("'");
                } else {
                    columnNames.append(",");
                    columnNames.append(f.getName());

                    columnValues.append(",");

                    if (f.get(this) == null) {
                        columnValues.append(f.get(this));
                    } else {
                        columnValues.append("'");
                        columnValues.append(f.get(this).toString().replaceAll("'", "\\\\'"));
                        columnValues.append("'");
                    }
                }
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Domain.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Domain.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        queryBuffer.append(columnNames);
        queryBuffer.append(") VALUES (");
        queryBuffer.append(columnValues);
        queryBuffer.append(")");
        return queryBuffer.toString();

    }
}
